package com.liankuan.console.controller;

import com.google.common.collect.ImmutableMap;
import com.liankuan.console.annotation.IgnoreSecurity;
import com.liankuan.console.common.BaseRsp;
import com.liankuan.console.common.ResultEnum;
import com.liankuan.console.controller.vo.request.User.UserDtoIO;
import com.liankuan.console.controller.vo.request.User.UserIO;
import com.liankuan.console.controller.vo.UserInfoVO;
import com.liankuan.console.controller.vo.request.User.UserStrategyIO;
import com.liankuan.console.controller.vo.user.UserListVO;
import com.liankuan.console.controller.vo.user.UserStrategyListVO;
import com.liankuan.console.dto.User;
import com.liankuan.console.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping(value = "/user")
public class UserController {
    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 用户的登陆验证
     *
     * @param userIO
     * @return
     */
    @IgnoreSecurity
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public BaseRsp login(@RequestBody UserIO userIO) {
        String token = userService.login(userIO.getUsername(), userIO.getPassword());
        return new BaseRsp<>(ImmutableMap.of("token", token));
    }

    /**
     * 获取用户信息
     *
     * @param token
     * @return
     */
    @RequestMapping("/info")
    public BaseRsp getInfo(String token) {
        UserInfoVO userInfoVO = userService.getInfo(token);
        return new BaseRsp<>(userInfoVO);
    }

    /**
     * 用户推出登陆状态
     *
     * @return
     */
    @RequestMapping("/logout")
    public BaseRsp logout(String token) {
        userService.logout(token);
        return new BaseRsp<>(ResultEnum.SUCCESS);
    }

    /**
     * 获取所以用户列表
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    public BaseRsp getUserList(Integer currentPage, Integer pageSize) {
        UserListVO items = userService.listUsers(currentPage, pageSize);
        return new BaseRsp<>(items);
    }


    /**
     * 获取所以用户对于某一个策略的对应关系
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/listUserStrategyInfo")
    public BaseRsp listUserStrategyInfo(Integer currentPage, Integer pageSize, Integer strategyId) {
        UserStrategyListVO items = userService.listUsersWithStrategy(currentPage, pageSize, strategyId);
        return new BaseRsp<>(items);
    }


    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteUser")
    public BaseRsp deleteUser(Integer id) {
        userService.deleteUser(id);
        return new BaseRsp<>(ResultEnum.SUCCESS);
    }


    /**
     * 更新用户信息
     *
     * @param userDtoIO
     * @return
     */
    @RequestMapping("/updateUser")
    public BaseRsp updateUser(@RequestBody UserDtoIO userDtoIO) {
        userService.updateUser(userDtoIO);

        return new BaseRsp<>(ResultEnum.SUCCESS);
    }

    /**
     * 新增用户信息
     *
     * @param userDtoIO
     * @return
     */
    @RequestMapping("/addUser")
    public BaseRsp addUser(@RequestBody UserDtoIO userDtoIO) {
        userService.addUser(userDtoIO);
        return new BaseRsp<>(ResultEnum.SUCCESS);
    }

    /**
     * 更新用户个人信息
     *
     * @param userDtoIO
     * @return
     */
    @RequestMapping(value = "/updateMyselfInfo", method = RequestMethod.POST)
    public BaseRsp updateMyselfInfo(@RequestBody UserDtoIO userDtoIO) {
        userService.updateMyselfInfo(userDtoIO);
        return new BaseRsp<>(ResultEnum.SUCCESS);
    }

    /**
     * 获取用户个人信息
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/getMyselfInfo", method = RequestMethod.GET)
    public BaseRsp getMyselfInfo(String token) {
        User user = userService.getUserInfoByToken(token);
        return new BaseRsp<>(user);
    }


    /**
     * 更新用户和某条策略之间的关系
     *
     * @param userStrategyIO
     * @return
     */
    @RequestMapping(value = "/updateUserStrategy", method = RequestMethod.POST)
    public BaseRsp updateUserStrategy(@RequestBody UserStrategyIO userStrategyIO) {
        userService.updateUserStrategy(userStrategyIO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }


    /**
     * 通过用户名判断用户是否存在
     *
     * @param name
     * @return
     */
    @RequestMapping(value = "/hasUserByName", method = RequestMethod.GET)
    public BaseRsp hasUserByName(String name) {
        boolean ishave = userService.getUserInfoByName(name);
        return new BaseRsp<>(ishave);
    }


}
